
import os
from typing import Optional
import PySide6.QtCore
import PySide6.QtWidgets
from PySide6.QtCore import Qt
from PySide6.QtWidgets import QDialog

from .Ui_ShowMd import Ui_ShowMd


class DialogShowMd(QDialog):
    def __init__(
        self, 
        md_path: 'str',
        parent: Optional[PySide6.QtWidgets.QWidget] = None, 
        f: PySide6.QtCore.Qt.WindowFlags = Qt.Widget) -> None:
        ########################################
        super().__init__(parent=parent, f=f)
        self._mdFilePath = md_path
        self._ui = Ui_ShowMd()
        self._ui.setupUi(self)
        f_ = open(self._mdFilePath, 'r', encoding='utf8')
        self._context = f_.readlines()
        if (self._context[len(self._context) - 1] == 'ShowNext: 1'):
            self._context.pop()
            self._ui.textBrowser.setMarkdown(''.join(self._context))
            self._ui.pushButton.clicked.connect(self.reject)
            self._ui.checkBox.stateChanged.connect(self._CheckStateChanged)
            f_.close()

    def _CheckStateChanged(self, state: 'int') -> None:
        check_state_ = Qt.CheckState(state)
        f_ = open(self._mdFilePath, 'w', encoding='utf8')
        if (check_state_ == Qt.Checked):
            # 设置为0
            self._context.append('ShowNext: 0')
            f_.write(''.join(self._context))
            self._context.pop()
        elif (check_state_ == Qt.Unchecked):
            self._context.append('ShowNext: 1')
            f_.write(''.join(self._context))
            self._context.pop()
        f_.close()



